using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Text.RegularExpressions;
using System.Threading;
using System.Diagnostics;
namespace Test {
	class WebLogProc {
		static void MainX() {
			int days=20;
			List<KeyValuePair<string,string>> logFiles=new List<KeyValuePair<string,string>>();
			DAL dal=new DAL();
			//dal.NonQry("drop table WebLog");
			//dal.NonQry("create table WebLog(id int identity,page varchar(255),qs varchar(512),ip varchar(15),usr varchar(50),time datetime)");
			string[] servers="pon-wpwb01-cl01;pon-wpwb01-cl02".Split(';');
			//string[] servers="ws-test".Split(';');
			//string[] servers="pon-wdwb01".Split(';');

			foreach(string srv in servers)
			{
				DirectoryInfo dinf=new DirectoryInfo(string.Format(@"\\{0}\c$\WINDOWS\system32\LogFiles\W3SVC1\",srv));
				//DirectoryInfo dinf=new DirectoryInfo(string.Format(@"C:\inetpub\logs\LogFiles\W3SVC1",srv));
				foreach(FileInfo file in dinf.GetFiles("*.log"))
					logFiles.Add(new KeyValuePair<string,string>(file.Name,file.FullName));
			}
			logFiles.Sort(KVPKeyCompare);
			dal.NonQry("truncate table WebLog");
			Stopwatch sw=Stopwatch.StartNew();
			int cnt=0;
			foreach(KeyValuePair<string,string> pair in logFiles)
			{
				ThreadPool.QueueUserWorkItem(new WaitCallback(new WebLogReader(pair).Read),cnt);
				Interlocked.Increment(ref _numerOfThreadsNotYetCompleted);
				cnt++;
				if(cnt>=days*2)
					break;
			}
			_doneEvent.WaitOne();
			Console.WriteLine("THE END");
			Console.WriteLine(sw.Elapsed);
		}
		static int KVPKeyCompare(KeyValuePair<string,string> k1,KeyValuePair<string,string> k2) {
			return k2.Key.CompareTo(k1.Key);
		}
		private static ManualResetEvent _doneEvent=new ManualResetEvent(false);
		private static int _numerOfThreadsNotYetCompleted=0;
		class WebLogReader {
			KeyValuePair<string,string> _pair;
			DAL dal=new DAL();
			public WebLogReader(KeyValuePair<string,string> pair) {
				_pair=pair;
			}
			public void Read(object ob) {
				Stat s=new Stat();
				using(FileStream fileStream=new FileStream(_pair.Value,FileMode.Open,FileAccess.Read,FileShare.ReadWrite))
				using(StreamReader textReader=new StreamReader(fileStream))
				{
					string visitDateDay=_pair.Key.Substring(2,6);
					while(!textReader.EndOfStream)
					{
						string line=textReader.ReadLine();
						if(!line.StartsWith("#"))
						{
							string[] data=line.Split(' ');
							//if(data[5]!="-")
							//if(data[2].EndsWith(".asp"))
							//if(data[2].ToLower().Contains("warehousemis"))
							//if(data[2].ToLower().Contains("packstbonus.asp"))
							//if(data[2].ToLower().Contains("/report/"))	
							//if(data[2].ToLower().Contains("/callaudit/"))
							//if(data[2].ToLower().Contains("supdtlframe2.asp"))
							//if(data[2].ToLower().Contains("/directorypicking/"))
							if(data[7].ToLower().Contains("firefox"))
								if(data.Length==12)
								{

									//s.Add(data[7]);
									//if(Regex.IsMatch(data[3],"ID=(\\d+)"))
									{

										//string sdata=Regex.Match(data[3],"ID=(\\d+)").Groups[1].Value;

										//string sdata="";
										string sdata=data[11];



										string visitDate=visitDateDay+data[0].Replace(":","");
										DateTime dat=DateTime.ParseExact(visitDate,"yyMMddHHmmss",null);
										dal.NonQry("insert into WebLog(page,qs,ip,usr,time,dur,data,uagt)values('{0}','{1}','{2}','{3}',convert(datetime,'{4}',20),{5},'{6}','{7}')",data[2].Replace("'","''"),""/*data[3].Replace("'","''")*/,data[6],data[5].ToLower().Replace("next-plc\\",""),dat.ToString("yyyy-MM-dd HH:mm:ss"),data[11],sdata,data[7]);
									}
								}
						}
					}
					Console.WriteLine(ob+":"+_pair.Value);
					if(Interlocked.Decrement(ref _numerOfThreadsNotYetCompleted)==0)
						_doneEvent.Set();
				}
			}
		}
	}
}
//class Spider {
//  Dictionary<string,Dictionary<object,int>> dict=new Dictionary<string,Dictionary<object,int>>();
//  public Spider(string prefix) {
//  }
//  public int GetId(string key,object val) {
//    if(!dict.ContainsKey(key))
//      dict.Add(key,new Dictionary<object,int>());
//    if(!dict[key].ContainsKey(val))
//      dict[key].Add(val,dict[key].Count+1);
//    return dict[key][val];
//  }
//  public void AddRow(params object[] arr) {
//  }
//}
/*
		static void MainZ() {
			List<KeyValuePair<string,string>> logFiles=new List<KeyValuePair<string,string>>();
			DAL dal=new DAL();
			//dal.NonQry("drop table WebLog");
			//dal.NonQry("create table WebLog(id int identity,page varchar(255),qs varchar(512),ip varchar(15),usr varchar(50),time datetime)");
			string[] servers="pon-wpwb01-cl01;pon-wpwb01-cl02".Split(';');
			//string[] servers="pon-wdwb01".Split(';');
			//Spider sp=new Spider("visit");
			foreach(string srv in servers)
			{
				DirectoryInfo dinf=new DirectoryInfo(string.Format(@"\\{0}\c$\WINDOWS\system32\LogFiles\W3SVC1\",srv));
				//string[] lines=null;
				foreach(FileInfo file in dinf.GetFiles("*.log"))
					logFiles.Add(new KeyValuePair<string,string>(file.Name,file.FullName));
			}
			logFiles.Sort(KVPKeyCompare);
			dal.NonQry("truncate table WebLog");
			List<Thread> thLst=new List<Thread>();
			int cnt=0;
			foreach(KeyValuePair<string,string> pair in logFiles)
			{
				thLst.Add(new Thread(new WebLogReader(pair).Read));
				if(cnt++>150)
					break;
			}
			Stopwatch sw=Stopwatch.StartNew();
			foreach(Thread th in thLst)
				th.Start();
			foreach(Thread th in thLst)
				th.Join();
			Console.WriteLine("THE END");
			Console.WriteLine(sw.Elapsed);
		}
*/